package zxing.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.virtualapp.R;
import io.virtualapp.abs.ui.VActivity;
import io.virtualapp.bean.MakeBean;
import zxing.activity.adapter.MakeAdapter;
import zxing.activity.adapter.RingAdapter;

public class MakingRecordsActivity extends VActivity {
    @BindView(R.id.recy_list)
    RecyclerView recyList;
    private MakeAdapter adapter;
    private List<MakeBean> datas;
    @BindView(R.id.head_back)
    ImageView head_back;
    @BindView(R.id.head_center_title)
    TextView head_center_title;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_making_records);
        ButterKnife.bind(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyList.setLayoutManager(linearLayoutManager);
        initDatas();
        initAction();
    }

    public void initDatas() {
        head_center_title.setText("记录");
        head_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        datas = new ArrayList<>();
        datas = DataSupport.findAll(MakeBean.class);
        if (datas != null) {
            adapter = new MakeAdapter(MakingRecordsActivity.this, datas);
            recyList.setAdapter(adapter);
        }

    }

    protected void initAction() {
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter madapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.item_parent://整个item
                        Intent intent = new Intent(getApplicationContext(), MakingDetailsActivity.class);
                        intent.putExtra("time", datas.get(position).getName());
                        intent.putExtra("codedContent", datas.get(position).getTit());
                        startActivity(intent);
                        break;
                    default:
                        break;
                }
            }
        });
        adapter.setOnItemChildLongClickListener(new BaseQuickAdapter.OnItemChildLongClickListener() {
            @Override
            public boolean onItemChildLongClick(BaseQuickAdapter madapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.item_parent://整个item
                        AlertDialog.Builder builder = new AlertDialog.Builder(MakingRecordsActivity.this);
                        builder.setTitle("删除记录?");
                        builder.setMessage("您是否要删除此条记录?");
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                DataSupport.delete(MakeBean.class, datas.get(position).getId());
                                adapter.removeData(position);
                            }
                        });
                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        builder.show();
                        break;
                    default:
                        break;
                }
                return false;
            }
        });
    }


}
